package cn.wizzer.app.web.commons.ext.websocket.action.group;

import cn.wizzer.app.sys.modules.services.SysSensitiveService;
import cn.wizzer.app.user.modules.models.GroupMember;
import cn.wizzer.app.user.modules.models.enums.MsgTypeEnum;
import cn.wizzer.app.user.modules.models.enums.OprTypeEnum;
import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.user.modules.services.GroupLogService;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.JSONUtil;
import cn.wizzer.framework.util.ThreadUtil;
import cn.wizzer.framework.util.param.Param4MapVerify;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.apache.commons.lang3.EnumUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.List;
import java.util.Map;


/**
 * 群消息发送
 *
 * @author .
 */
@IocBean()
@NettyAction(uri = "/group/send", filters = {TokenFilter.class})
public class GroupSendAction extends BaseNettyAction {

    private static final Log log = Logs.get();

    @Inject
    private NettyMsgUtil nettyMsgUtil;
    @Inject
    @Reference
    private GroupLogService groupLogService;
    @Inject
    @Reference
    private SysSensitiveService sysSensitiveService;


    /**
     * @param income
     * @param params groupId    :群id
     *               content    :内容，如果是文件则走文件上传接口
     *               type       :类型 (  TEXT("文本"),IMG("图片"),VIDEO("视频文件"),AUDIO("音频文件"),OPERATE("操作"))
     * @return
     */
    @Override
    public Result action(Channel income,
                         @Param4MapVerify(fields = {@ParamVerify(name = "groupId", required = true),
                                 @ParamVerify(name = "content", required = true),
                                 @ParamVerify(name = "type", required = true)}) JSONObject params) {
        log.info("income:" + income);
        ChatUserVO sendUser = NettyCache.globalGet(params.get("appid"));
        //发送消息 -> 添加群消息记录 -> 创建用户读取群消息记录
        Map<String, Object> map = groupLogService.groupSend(sendUser.getId(), params.getString("groupId"), sysSensitiveService.filter(params.getString("content")), EnumUtils.getEnum(MsgTypeEnum.class, params.getString("type")), OprTypeEnum.SENT, false);
        log.debug("发送记录返回的map:" + map);
        List<GroupMember> groupMemberList = JSONObject.parseArray(JSONObject.toJSONString(map.get("groupMemberList")), GroupMember.class);
        log.debug("打印出群成员groupMemberList:" + groupMemberList);
        Object groupLog = map.get("groupLog");
        String content = JSONUtil.toJSONString(groupLog);
        ThreadUtil.execute(() -> {
            // 打印出localMap
            log.debug("打印出localMap" + NettyCache.getLocalMap());
            log.debug("打印出map" + NettyCache.getMap());
            //给群里在线的用户发送提醒
            for (GroupMember groupMember : groupMemberList) {
                log.debug("打印出要发送的人groupMember:" + groupMember);
                ChatUserVO receiveUser = NettyCache.globalGet(groupMember.getUserId());
                log.debug("打印出接收人是否在线receiveUser:" + receiveUser);
                // 判断人员是否在线
                if (receiveUser != null) {
                    nettyMsgUtil.send(receiveUser.getChannel(), ChatResult.MessageTypeEnum.USER, ChatResult.ActionEnum.SEND, sendUser, content);
                }
            }
        });
        return Result.success();
    }
}
